const { AppError } = require('../middlewares/errorHandler');
const { logger } = require('../utils/logger');
const queueService = require('../services/queue.service');

class QueueController {
  /**
   * 获取队列状态
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async getQueueStatus(ctx) {
    const { queueName } = ctx.params;
    
    try {
      const status = await queueService.getQueueStatus(queueName);
      ctx.body = {
        status: 'success',
        data: { status }
      };
    } catch (error) {
      throw new AppError(404, `Queue ${queueName} not found`);
    }
  }

  /**
   * 获取队列任务
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async getJob(ctx) {
    const { queueName, jobId } = ctx.params;

    try {
      const job = await queueService.getJob(queueName, jobId);
      if (!job) {
        throw new AppError(404, 'Job not found');
      }

      ctx.body = {
        status: 'success',
        data: { job }
      };
    } catch (error) {
      if (error instanceof AppError) throw error;
      throw new AppError(404, `Queue ${queueName} not found`);
    }
  }

  /**
   * 添加队列任务
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async addJob(ctx) {
    const { queueName } = ctx.params;
    const { data, options } = ctx.request.body;

    try {
      const job = await queueService.addJob(queueName, data, options);
      ctx.status = 201;
      ctx.body = {
        status: 'success',
        data: { job }
      };
    } catch (error) {
      throw new AppError(404, `Queue ${queueName} not found`);
    }
  }

  /**
   * 移除队列任务
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async removeJob(ctx) {
    const { queueName, jobId } = ctx.params;

    try {
      const removed = await queueService.removeJob(queueName, jobId);
      if (!removed) {
        throw new AppError(404, 'Job not found');
      }

      ctx.body = {
        status: 'success',
        message: 'Job removed successfully'
      };
    } catch (error) {
      if (error instanceof AppError) throw error;
      throw new AppError(404, `Queue ${queueName} not found`);
    }
  }

  /**
   * 暂停队列
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async pauseQueue(ctx) {
    const { queueName } = ctx.params;

    try {
      await queueService.pauseQueue(queueName);
      ctx.body = {
        status: 'success',
        message: 'Queue paused successfully'
      };
    } catch (error) {
      throw new AppError(404, `Queue ${queueName} not found`);
    }
  }

  /**
   * 恢复队列
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async resumeQueue(ctx) {
    const { queueName } = ctx.params;

    try {
      await queueService.resumeQueue(queueName);
      ctx.body = {
        status: 'success',
        message: 'Queue resumed successfully'
      };
    } catch (error) {
      throw new AppError(404, `Queue ${queueName} not found`);
    }
  }

  /**
   * 清理队列
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async cleanQueue(ctx) {
    const { queueName } = ctx.params;
    const { grace } = ctx.request.body;

    try {
      await queueService.cleanQueue(queueName, grace);
      ctx.body = {
        status: 'success',
        message: 'Queue cleaned successfully'
      };
    } catch (error) {
      throw new AppError(404, `Queue ${queueName} not found`);
    }
  }

  /**
   * 获取所有队列
   * @param {Object} ctx - 上下文
   * @returns {Promise<void>}
   */
  async getAllQueues(ctx) {
    const queues = queueService.getAllQueues();
    const queueNames = Object.keys(queues);

    ctx.body = {
      status: 'success',
      data: { queues: queueNames }
    };
  }
}

module.exports = new QueueController(); 